package com.top.mall.utils;

import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import android.util.Log;

public class SuperScriptTextSpan extends MetricAffectingSpan {
    private int fontSizeSp = -1;
    private boolean isSp;

    public SuperScriptTextSpan(int fontSizeSp, boolean isSp) {
        this.fontSizeSp = fontSizeSp;
        this.isSp = isSp;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        float ascent = ds.ascent();
        Log.d("SuperScriptTextSpan", "ascent-> " + ds.ascent());
        if (fontSizeSp != -1) {
            ds.setTextSize(isSp ? fontSizeSp * ds.density : fontSizeSp);
        }
        float newAscent = ds.ascent();
        Log.d("SuperScriptTextSpan", "newAscent-> " + ds.ascent());

        ds.baselineShift += ascent - newAscent;

        //方法2：
        //Rect bounds = new Rect();
        //ds.getTextBounds("1A", 0, 2, bounds);
        //int shift = bounds.top - bounds.bottom;
        //if (fontSizeSp != -1) {
        //    ds.setTextSize(isSp ? fontSizeSp * ds.density : fontSizeSp);
        //}
        //ds.getTextBounds("1A", 0, 2, bounds);
        //shift += bounds.bottom - bounds.top;
        //ds.baselineShift += shift;
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        updateDrawState(p);
    }
}